/**
 * Created with JetBrains PhpStorm.
 * User: Evan Hou
 * Date: 12-8-12
 * Time: 上午12:39
 * To change this template use File | Settings | File Templates.
 */

var wq = {
    'base_url':'http://tomduiyi.cn/',
    'img_url':'http://tomduiyi.cn/',
    'static_url':'http://tomduiyi.cn/',
    'cookie_prefix':'wq_',
    'cookie_domain':'tomduiyi.cn',
    'cookie_path':'/'
};


// 获取Cookie
wq.getCookie = function (name)
{
    name = wq.cookie_prefix+name;

    var arrStr = document.cookie.split("; ");

    for (var i = 0; i < arrStr.length; i++) {
        var temp = arrStr[i].split("=");
        if (temp[0] == name) return unescape(temp[1]);
    }

    return null;
}


// 设置Cookie
wq.setCookie = function (name, value, expires)
{
    var secure;
	var today = new Date();
	today.setTime(today.getTime());
	if (expires) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date(today.getTime() + (expires));
	document.cookie = wq.cookie_prefix+name+'='+escape(value) +
	( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
	( ( wq.cookie_path ) ? ';path=' + wq.cookie_path : '' ) +
	( ( wq.cookie_domain ) ? ';domain=' + wq.cookie_domain : '' ) +
	( ( secure ) ? ';secure' : '' );
}

// 删除Cookie
wq.deleteCookie = function (name, path, domain)
{
	if (getCookie(name)) document.cookie = wq.cookie_prefix+name + '=' +
	( ( path ) ? ';path=' + path : '') +
	( ( domain ) ? ';domain=' + domain : '' ) +
	';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

// 删除数组包含某元素，并重新构建索引
wq.arr_del = function (arr, d)
{
	return arr.slice(0,d-1).concat(arr.slice(d));
}

// 数组查找返回索引
wq.arr_find = function (arr, d)
{
	var length = arr.length;
	for (var i = 0; i < length; i++) {
		if (arr[i] == d) {
			return i;
		}
	}
	return -1;
}

/* 是否存在于数组 */
wq.inArray = function (value, array)
{
  for (var i=0; i < array.length; i++) {
      if (array[i] == value) return true;
  }
  return false;
}

//AJAX请求
wq.ajax = function (url, parameter)
{
    var msg;
	$.ajax({
	   type : "POST",
	   url  : wq.base_url+url,
	   async: false,
	   data : parameter,
       dataType:'json',
	   success: function(msgs){
			//eval('('+msgs+')');
           msg = msgs;
	   }
	});
	return msg;
}

wq.jsonp = function(url, param, func, _async)
{
    if((typeof _async) == 'undefined')
    {
        _async = true;
    }

    //var result = {};
    //$.getJSON(url + "?"+ $.param(param)+"&callback=?", func); return;

    //var msg;
	$.ajax({
	   type : "GET",
	   url  : wq.base_url+url+"?callback=?",
	   async: _async,
	   data : (param),
       dataType:'json',
	   success: func
	});

	//return msg;
}

//获取单选框的值
wq.getRadioCheckBoxValue = function (RadioName)
{
	var obj;
	obj=document.getElementsByName(RadioName);
	if(obj!=null){
	    var i;
	    for(i=0;i<obj.length;i++){
	        if(obj[i].checked){
	            //return true;
                return obj[i].value;
	        }
	    }
	}
	return false;
}

//获取当前URL的某个参数值
wq.request = function (paras){
	var url = location.href;
	var paraString = url.substring(url.indexOf("?")+1,url.length).split("&");
	var paraObj = {}
	for (i=0; j=paraString[i]; i++)
	{
		paraObj[j.substring(0,j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=")+1,j.length);
	}
	var returnValue = paraObj[paras.toLowerCase()];
	if(typeof(returnValue)=="undefined")
	{
		return "";
	}else{
		return returnValue;
	}
}

//是否为邮件地址
wq.isEmail = function ( str ){
    var myReg = /^[-_A-Za-z0-9]+@([_A-Za-z0-9]+\.)+[A-Za-z0-9]{2,3}$/;

    return myReg.test(str) ? true : false;
}

//判断是否为空
wq.isEmpty = function(content)
{
    return (content == '' || content == undefined) ? false : true;
}

//判断长度
wq.limit_length = function (str, minLength, maxLength)
{
    if (!wq.isEmpty (str)) {
        return false;
    }

    if (!wq.isEmpty (minLength)) {
        minLength = 6;
    }

    if (!wq.isEmpty (maxLength)) {
        maxLength = 32;
    }

    if (str.length < minLength || str.length > maxLength) {
        return false;
    }

    return true;
}


//获取来源(referer)地址
wq.getReferer = function ()
{
    var referer = document.referrer;

    return wq.isEmpty(referer) ? referer : wq.base_url;
}

//跳转到上一个页面
wq.goToBack = function ()
{
    var url = wq.getReferer();

    if (wq.isEmpty(url) && wq.isUrl(url)) {
        window.location.href = url;
    }

    return false;
}

//收藏
wq.addFavorite = function (title, url)
{
    title = wq.isEmpty(title) ? title : "TOM对弈围棋, www.tomduiyi.com";
    url = wq.isEmpty(url) ? url : wq.base_url+"?fav";

	if(window.sidebar) {
		window.sidebar.addPanel(title,url,'');
	} else {
		try{
			window.external.AddFavorite(url,title);
		} catch(e) {
			alert("您的浏览器不支持该功能,请使用Ctrl+D收藏本页");
		}
	}
}

//设为首页
wq.setHomepage = function () {
    if (document.all) {
        document.body.style.behavior = 'url(#default#homepage)';
        document.body.setHomePage('http://www.tomduiyi.com');
    } else if (window.sidebar) {
        if (window.netscape) {
            try {
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            }
            catch (e) {
                alert("该操作被浏览器拒绝，如果想启用该功能，请在地址栏内输入 about:config,然后将项 signed.applets.codebase_principal_support 值该为true");
            }
        }
        var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
        prefs.setCharPref('browser.startup.homepage', 'http://www.tomduiyi.com');
    }
}


//跳转到某个地址
wq.goToUrl = function (url)
{
    //url = wq.base_url+url;

    url = url.split('#');
    url = url[0];
    /*
    if (wq.isUrl(url) ) {
        alert ('不是一个正确的URL地址!');
        return false;
    }
    //*/

    window.location.href = url;
}

//让页面重新加载
wq.pageReload = function (time)
{
    if (time) {
        time = time * 1000;
        setTimeout('window.location.reload()', time);
    } else {
        window.location.reload();
    }
}

//验证手机号码
wq.isMobile = function (value) {
    if (/^13\d{9}$/g.test(value) || (/^15[0-35-9]\d{8}$/g.test(value)) || (/^18[05-9]\d{8}$/g.test(value))) {
        return true;
    } else {
        return false;
    }
}

//判断输入是否为中文
wq.isChinese = function(s) {
    var ret = true;
    for (var i = 0; i < s.length; i++)
        ret = ret && (s.charCodeAt(i) >= 10000);
    return ret;
}